package com.example.yls.demoa;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private AnimationDrawable animationDrawable;
    private ImageView imageView;
    private Button btn_start;
    private Button btn_start_s;
    private Button btn_start_t;
    private Button ban_stop;
    private ImageView iv_dog;
    private ImageView s_dog;
    private ImageView t_dog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.wifi);
        btn_start=findViewById(R.id.btn_start);
        ban_stop=findViewById(R.id.btn_stop);
        iv_dog=findViewById(R.id.iv_dog);
        s_dog=findViewById(R.id.s_dog);
        t_dog=findViewById(R.id.t_dog);
        btn_start_s=findViewById(R.id.btn_start_s);
        btn_start_t=findViewById(R.id.btn_start_t);


        animationDrawable= (AnimationDrawable) imageView.getDrawable();
        btn_start_s.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                srartScalAnimation();
            }
        });
        btn_start_t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startTAnimation();
            }
        });

        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    startAlphaAnimation();





            }
        });
        ban_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* animationDrawable.stop();*/
            }
        });
    }

    private void srartScalAnimation() {
        ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3,26,26);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatCount(-1);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setRepeatMode(TranslateAnimation.REVERSE);
        s_dog.startAnimation(scaleAnimation);
    }

    private void startTAnimation() {
        //使用xml方式播放ScaleAnimation
        TranslateAnimation translateAnimation=new TranslateAnimation(0,300,0,300);
        translateAnimation.setFillAfter(true);
        translateAnimation.setRepeatCount(-1);
        translateAnimation.setDuration(1000);
        translateAnimation.setRepeatMode(TranslateAnimation.REVERSE);
        t_dog.startAnimation(translateAnimation);
    }

    private void startAlphaAnimation() {

        //使用xml方式播放alpha动画
               /* AlphaAnimation alphaAnimation= (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha_anim);
*/
        //使用代码方式播放alpha动画
               AlphaAnimation alphaAnimation= new AlphaAnimation(1.0f,0.0f);
               alphaAnimation.setFillAfter(true);
               alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
               alphaAnimation.setRepeatCount(-1);
               alphaAnimation.setDuration(1000);
               iv_dog.startAnimation(alphaAnimation);

    }
}
